package com.yanqu.road.server.manger.relatives.task;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.relatives.RelativesInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.pb.relatives.RelativesProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.relatives.RelativesModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.relatives.RelativesMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;

public class CancelRelativesTask extends GeneralTask {

    long userId;

    long relativesId;

    long relativesServerId;

    String params;

    public CancelRelativesTask(long userId, long relativesId, long relativesServerId, String params) {
        this.userId = userId;
        this.relativesId = relativesId;
        this.relativesServerId = relativesServerId;
        this.params = params;
    }

    @Override
    public void doRun() {
        boolean load = RelativesMgr.isLoadUserRelatives(userId);
        if(!load){
            RelativesMgr.loadRelativesInfo(userId);
        }

        RelativesMgr.getLogger().info("cross relatives cancel {}, {}", userId, relativesId);
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        UserInfo userInfo;
        RelativesInfo info;

        //解散发起者
        UserBaseInfo userBaseInfo = RelativesMgr.cacheOtherServerUserMap.get(relativesId);
        if(player != null){
            player.getModule(RelativesModule.class).deleteLetter(relativesId);
            player.getModule(RelativesModule.class).deleteGift(relativesId);
            player.getModule(RelativesModule.class).syncRelativesData();
            player.notifyListener(eGamePlayerEventType.RelativesCount.getValue(), RelativesMgr.getRelativesNum(userId));
            userInfo = player.getUserInfo();
            info = player.getModule(RelativesModule.class).getRelativesInfo(relativesId);
        }else {
            userInfo = UserMgr.getUserInfo(userId);
            info = RelativesMgr.getRelativesInfo(userId, relativesId);
        }

        if(info != null){
            info.setStatus((short) 0);
            info.setMarriageTimes(0);
            info.setLetterNum1(0);
            info.setLetterNum2(0);
            info.setIsShow("0,0");
        }

        String serverName = "";
        ServerInfo serverInfo = ServerListMgr.getServerInfo(relativesServerId);
        if(serverInfo != null){
            serverName = ServerLanguageMgr.getContent(serverInfo.getServerName(), userInfo.getLanguage());
        }

        String mailTitle = MultipleLanguageMgr.getContent(MailManager.RELATIVES_CANCEL_TITLE, userInfo.getLanguage());
        String  mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.RELATIVES_CROSS_CANCEL_CONTENT, userInfo.getLanguage()), serverName, userBaseInfo == null ? "" : userBaseInfo.getNickName());
        MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), "", mailContent, mailTitle);

        RelativesProto.CrossCancelRelativesRespMsg.Builder respMsg = RelativesProto.CrossCancelRelativesRespMsg.newBuilder();
        RelativesProto.CrossCancelRelativesReqMsg.Builder reqMsg = RelativesProto.CrossCancelRelativesReqMsg.newBuilder();
        reqMsg.setRelativesId(relativesId);
        reqMsg.setToServerId(relativesServerId);
        reqMsg.setFromUserId(userId);
        reqMsg.setFromServerId(GameServer.getInstance().getServerId());
        respMsg.setRet(0);
        respMsg.setReq(reqMsg);
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_RELATIVES_CANCEL_BACK, respMsg));
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public long getRelativesId() {
        return relativesId;
    }

    public void setRelativesId(long relativesId) {
        this.relativesId = relativesId;
    }

    public long getRelativesServerId() {
        return relativesServerId;
    }

    public void setRelativesServerId(long relativesServerId) {
        this.relativesServerId = relativesServerId;
    }

    public String getParams() {
        return params;
    }

    public void setParams(String params) {
        this.params = params;
    }
}
